package haupt.dev.NeocronServiceQuery;

import info.neocron.service.PublicInterface.ArrayOfExtendedOutpost;
import info.neocron.service.PublicInterface.ArrayOfServerStatistic;
import info.neocron.service.PublicInterface.ExtendedOutpost;
import info.neocron.service.PublicInterface.OutpostListResult;
import info.neocron.service.PublicInterface.PublicInterface;
import info.neocron.service.PublicInterface.PublicInterfaceSoap;
import info.neocron.service.PublicInterface.ServerStatistic;
import info.neocron.service.PublicInterface.ServerStatisticResult;

import java.util.List;

import org.pmw.tinylog.Logger;

public class InterfaceManager {

	private static PublicInterfaceSoap publicInterfaceSOAP;
	
	public static List<ServerStatistic> getServerStatistic(String token) {
		initializePublicInterface();
		
		ServerStatisticResult serverStatistics = publicInterfaceSOAP.getServerStatistics(token);
		ArrayOfServerStatistic arrayOfServerStatistics = serverStatistics.getServerStatistics();
		List<ServerStatistic> serverStatisticList = arrayOfServerStatistics.getServerStatistic();
		
		if(serverStatisticList == null || serverStatisticList.isEmpty()) {
			Logger.warn("No valid result for ServerStatistics");
		}
		return serverStatisticList;
	}
	
	@SuppressWarnings("finally")
	public static List<ExtendedOutpost> getExtendedOutposts(String token, String serverName) {
		initializePublicInterface();
		
		List<ExtendedOutpost> extendedOutpostList = null;
		try {
			OutpostListResult outposts = publicInterfaceSOAP.getOutposts(token, serverName);
			ArrayOfExtendedOutpost arrayOfExtendedOutposts = outposts.getOutposts();
			extendedOutpostList = arrayOfExtendedOutposts.getExtendedOutpost();
		} catch (NullPointerException e) {
			Logger.error(e);
		} finally {
			return extendedOutpostList;
		}
	}
	
	public static void initializePublicInterface() {
		if(publicInterfaceSOAP == null) {
			PublicInterface publicInterface = new PublicInterface();
			publicInterfaceSOAP = publicInterface.getPublicInterfaceSoap();
		}
	}
}
